<?php
session_start();

// Return json encoded data
header ('Content-type: application/json');

/**
 * Script used to delete an element in the database
 */

require_once '../core/db.php';

// Delete the page (if the logged in user owns it):
if(isset($_SESSION['user'])) {
    $sql = 'DELETE FROM siteElement WHERE elementId=? AND siteUserId=?';
    $sth = $db->prepare ($sql);
    $res = $sth->execute (array ($_POST['id'], $_SESSION['user']));
    if ($res==1) {
        echo json_encode (array ('ok'=>'OK', 'message'=>'Elementet ble slettet fra databasen'));
    }
    else {
        echo json_encode (array ('message'=>'Kunne ikke slette elementet.'));
    }
}
else {
    echo json_encode (array ('message'=>'Du kan ikke slette et element uten å være logget inn.'));
}
?>